module.exports = function(grunt) {

    // Project configuration.
    grunt.initConfig({
        pkg: grunt.file.readJSON('./package.json'),
        jshint: {
            options: {
                newcap: false
            },
            all: ['Gruntfile.js', 'public/javascripts/app/**/*.js', 'public/javascripts/app.js', 'public/javascripts/main.js', 'public/javascripts/module.js', 'public/javascripts/routes.js'],
            min: ['public/minjs/**/*.js']
        },
        cssmin: {
            minify: {
                files: [{
                    expand: true,
                    cwd: './public/StyleSheets/',
                    src: ['**/*.css','!**/leaflet-draw.css', '!myStyle/**/*.css'],
                    dest: './public/dist/StyleSheets/'
                }]
            },
            combine: {
                files: {
                    'public/dist/style.min.css': [
                        'public/dist/StyleSheets/**/*.css'
                    ]
                }
            }
        },
        groc: {
            javascript: [
                "public/javascripts/app/**/*.js", "README.md"
            ],
            options: {
                "out": "doc/"
            }
        }
    });

    grunt.registerTask('mani', 'My Task to change manifest file version Number', function() {
        var done = this.async();
        var path = require('path'),
            fs = require('fs');
        var manifest_path = path.join(__dirname, './public/manifest.appcache');
        fs.readFile(manifest_path, function(err, data) {
            if (err) {
                console.log(err);
            }
            var str = data.toString();
            var reg1 = /#.+/;
            var str1 = str.match(reg1)[0];
            var rep1 = "# " + new Date().toString();

            var reg2 = /# version.+/;
            var str2 = str.match(reg2)[0];
            var rep2 = "# version:" + (parseFloat(str2.split(':')[1]) + 0.0001).toString();
            str = str.replace(str1, rep1);
            str = str.replace(str2, rep2);
            fs.writeFile(manifest_path, str, function(err) {
                done();
            });
        });
    });

    // Load the plugin that provides the "uglify" task.
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-cssmin');
    grunt.loadNpmTasks('grunt-contrib-jshint');

    // Default task(s).

    grunt.registerTask('hint', ['jshint:all']);
    grunt.registerTask('minmin', ['cssmin:minify', 'cssmin:combine']);
    grunt.registerTask('default', ['minmin', 'mani']);
    grunt.registerTask('doc', ['groc']);
};
